<?php


namespace app\reception\controller;


use app\common\JwtUtil;
use app\model\Users;
use app\Shop\controller\Register;
use think\facade\Session;

const URL = 'https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&';
class Login {
	function index() {
		$input = input('post.');
		if (empty($input['account']) || empty($input['password'])) {
			return json(['status' => false, 'msg' => '账号或密码不能为空']);
		}
		$lg = \app\model\Login::where(['account' => $input['account']])->select();
		if (empty($lg['0'])) {
			//用户列表找不到便去传递给商户后台进行判断
			$login = \app\Shop\controller\Login::index($input);
			return $login;
		}
		if ($lg['0']['password'] != $input['password']) {
			return json(['status' => false, 'msg' => '密码错误']);
		}
		$user = Users::where(['login_id' => $lg['0']['id']])->select();
		$token = JwtUtil::encode($input);
		\session('identity', $user['0']['identity']);
		return json(['status' => true, 'token' => $token, 'type' => true, 'user' => $user, 'account' => $input['account']]);
	}

	//微信端登录
	function wx_login() {
		$input = input('post.');
		try {
			if (!$input['appid']) {
				throw new \Exception('未获取到appid数据]');
			}
			if (!$input['secret']) {
				throw new \Exception('未获取到secret数据]');
			}
			if (!$input['code']) {
				throw new \Exception('未获取到code数据]');
			}
			$login = \app\model\Login::where(['account' => $input['account']])->select();
			if (empty($login['0'])) {
				//用户列表找不到便去传递给商户后台进行判断
				return \app\Shop\controller\Login::index($input);
			}
			if (empty($input['account']) || empty($input['password'])) {
				return json(['status' => false, 'msg' => '账号或密码不能为空']);
			}
			if ($login['0']['password'] != $input['password']) {
				return json(['status' => false, 'msg' => '密码错误']);
			}
			$user = Users::where(['login_id' => $login['0']['id']])->select();
			$url = URL . 'appid=' . $input['appid'] . '&secret=' . $input['secret'] . '&js_code=' . $input['code'];
			$openid = json_decode(file_get_contents($url))->openid;
			if ($login['0']['openid'] == null) {
				\app\model\Login::where(['id' => $login['0']['id']])->update(['openid' => $openid]);
			}
			$token = JwtUtil::encode(['openid' => $openid]);
			\session('identity', $user['0']['identity']);
			return json(['status' => true, 'token' => $token, 'type' => true, 'user' => $user]);
		} catch (\Exception $e) {
			return json(['status' => false, 'error' => $e->getMessage()]);
		}
	}

	//用户注册
	function register() {
		$input = input('post.');
		if (empty($input['account']) || empty($input['password'])) {
			return json(['status' => false, 'msg' => '不能为空']);
		}
		if ($input['bool']) {
			$register = Register::index($input);
			return $register;
		} else {
			try {
				$lg = \app\model\Login::getByAccount($input['account']);
				if (!empty($lg)) {
					throw new \Exception('用户已存在');
				}
				\app\model\Login::insert([
					'account' => $input['account'],
					'password' => $input['password']
				]);
				$login = \app\model\Login::where(['account'=>$input['account']])->select();
				Users::insert([
					'name' => $input['name'],
					'login_id' => $login['0']['id'],
					'address' => $input['address']
				]);
				return ['status' => true,];
			} catch (\Exception $e) {
				return json(['status' => false, 'error' => $e->getMessage()]);
			}
		}
	}
	//注销
	function zx() {
		Session::set('status', null);
		return json(['status' => true, 'msg' => '注销成功']);
	}
}